import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  UseGuards,
  SetMetadata
} from '@nestjs/common'
import { GuardService } from './guard.service'
import { CreateGuardDto } from './dto/create-guard.dto'
import { UpdateGuardDto } from './dto/update-guard.dto'
import { RoleGuard } from './role/role.guard'
import { Role } from './role/role.decorator'
import { ReqUrl } from './role/role.decorator'
import { ApiTags,ApiOperation ,ApiParam ,ApiQuery,ApiResponse,ApiBearerAuth } from '@nestjs/swagger/dist'
@Controller('guard')
@ApiBearerAuth()
@ApiTags('守卫接口')
@UseGuards(RoleGuard)
export class GuardController {
  constructor(private readonly guardService: GuardService) {}

  @Post()
  create(@Body() createGuardDto: CreateGuardDto) {
    return this.guardService.create(createGuardDto)
  }

  @Get()
  @ApiOperation({summary:'get接口',description:"只有admin和bbigger004才可以访问"})
  @ApiQuery({ name:'role',description:'角色参数 只有admin 和bbigger004 可以访问',enum:['admin','bbigger004']})
  @Role('admin', 'bbigger004')
  @ApiResponse({ status:403, description: '小黑子  我是403' })
  findAll(@ReqUrl('123') url: string) {
    console.log('url',url)
    return this.guardService.findAll()
  }

  @Get(':id')
  @ApiParam({ name: 'id', description: '传入id', type: 'integer' })
  @ApiQuery({ name:'role',description:'角色参数 只有admin 和bbigger004 可以访问'})
  @SetMetadata('role',['admin','bbigger004'])
  findOne(@Param('id') id: string) {
    return this.guardService.findOne(+id)
  }

  @Patch(':id')
  update(@Param('id') id: string, @Body() updateGuardDto: UpdateGuardDto) {
    return this.guardService.update(+id, updateGuardDto)
  }

  @Delete(':id')
  @SetMetadata('role',['admin','bbigger004'])
  @ApiQuery({ name:'role',description:'角色参数 只有admin 和bbigger004 可以访问'})
  @ApiResponse({ status: 403, description: '小黑子  我是403' })
  remove(@Param('id') id: string) {
    return this.guardService.remove(+id)
  }
}
